package chatNI;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


/* 
 * @author Vansh and deepali
 */
public class ThreadSendFile extends Thread {

    /**
     * @param cni consists of the instance of the chat network that we are using
     * to receive this file
     * @param fich Consists of the file that we will create for the received
     * file the file is in the home directory by default
     * @param addr consists of the address of the user to whom we intent to send
     * the message
     * @param port consists of the port for this application
     * @param username consists of the user name of the person to whom we will
     * send the file
     *
     */
    private static ChatNI cni;
    private int port;
    private File fich;
    private InetAddress addr;
    private String username;

    /**
     * This class extends a thread and allows us to send a file through TCP
     * connection
     */
    public ThreadSendFile(String username, ChatNI cni, InetAddress addr_dest, int port, File fich) throws UnknownHostException {
        this.username = username;
        this.port = port;
        this.cni = cni;
        this.addr = addr_dest;
        this.fich = fich;
    }

    public void run() {
        System.out.println("Running ThreadSendFile");
        Socket sndSock = null;
        BufferedOutputStream output = null;
        BufferedInputStream input = null;
        try {
            System.out.println(addr + "" + port);
            sndSock = new Socket(addr, port);

            System.out.println("sndSock in ThreadSendFile Created");
            output = new BufferedOutputStream(sndSock.getOutputStream());
            input = new BufferedInputStream(new FileInputStream(fich));
            System.out.println(fich);

            System.out.println("Input Output Stream SendFIle created");
            byte[] octets = new byte[1024];

            int oc = input.read(octets, 0, 1024);
            System.out.println("OC:" + oc);
            output.write(octets, 0, oc);
            do {
                System.out.println("Writing");
                output.write(octets, 0, oc);
            } while ((oc = input.read(octets, 0, 1024)) > -1);
            System.out.println("Written");
            output.flush();
            System.out.println("Flushed");
            output.close();
            sndSock.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.flush();
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        cni.fileSent(username);
        System.out.println("Ended ThreadSendFile");
    }

}
